package tu.java.movie;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;

import tu.java.db.DBConnManager;

public class Movie {
	
	
	
	private int					movieId;
	private String				activeFromDate;
	private String				activeToDate;
	private String				movieName;
	private String				movieDescription;
	private int					movieTypeId;
	private String				movieType;
	private int					movieLenght;
	private Vector<Genre>		genres;
	private Vector<MovieImage>	movieImages;
	
	
	
	public static Vector<Movie> searchMovies(String movieName, int movieId) {
		Vector<Movie> movies = new Vector<Movie>();
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBConnManager.getInstance().getConnection();
			StringBuilder sql = new StringBuilder();
			sql.append(" SELECT CM01_MOVIE_ID,CM01_MOVIE_NAME,CM01_MOVIE_DESCRIPTION,CM01_MOVIE_LENGHT_MINUTES,");
			sql.append(" CS01_MOVIE_TYPE,CS01_DESCRIPTION");
			sql.append(" FROM TM01_MOVIES,TS01_NOMENCLATURES WHERE CS01_MOVIE_TYPE=CS01_NOMENCLATURE_ID");
			if (movieId != 0) {
				sql.append(" AND CM01_MOVIE_ID=?");
			}
			else if (movieName!=null && !movieName.trim().equals("")){
				sql.append(" AND CM01_MOVIE_NAME LIKE ?");
			}
			
			pstmt = conn.prepareStatement(sql.toString());
			if (movieId != 0) {
				pstmt.setInt(1, movieId);
			}
			else if (movieName!=null && !movieName.trim().equals("")) {
				pstmt.setString(1, movieName);
			}
			rs = pstmt.executeQuery();
			while (rs.next()) {
				Movie m = new Movie();
				m.movieId = rs.getInt(1);
				m.movieName = rs.getString(2);
				m.movieDescription = rs.getString(3);
				m.movieLenght = rs.getInt(4);
				m.movieTypeId = rs.getInt(5);
				m.movieType = rs.getString(6);
				m.genres = Genre.searchGenreForMovie(m.movieId);
				m.movieImages = MovieImage.loadAllMovieImages(m.movieId);
				movies.add(m);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			DBConnManager.getInstance().close(rs);
			DBConnManager.getInstance().close(pstmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		return movies;
	}
	
	
	
	public boolean addMovie(int userId) {
		boolean status = true;
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			conn = DBConnManager.getInstance().getConnection();
			StringBuilder sql = new StringBuilder();
			sql.append(" INSERT INTO TM01_MOVIES (CM01_MOVIE_NAME,CM01_MOVIE_DESCRIPTION,CM01_MOVIE_LENGHT_MINUTES,");
			sql.append(" CS01_MOVIE_TYPE,CS02_USER_ID) VALUES (?,?,?,?,?)");
			pstmt = conn.prepareStatement(sql.toString());
			pstmt.setString(1, movieName);
			pstmt.setString(2, movieDescription);
			pstmt.setInt(3, movieLenght);
			pstmt.setInt(4, movieTypeId);
			pstmt.setInt(5, userId);
			pstmt.executeUpdate();
		}
		catch (Exception e) {
			status = false;
			e.printStackTrace();
		}
		finally {
			DBConnManager.getInstance().close(pstmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		return status;
	}
	
	
	
	public boolean editMovie() {
		boolean status = true;
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			conn = DBConnManager.getInstance().getConnection();
			StringBuilder sql = new StringBuilder();
			sql.append(" UPDATE TM01_MOVIES SET CM01_MOVIE_NAME = ?, CM01_MOVIE_DESCRIPTION = ?,");
			sql.append(" CM01_MOVIE_LENGHT_MINUTES = ?, CS01_MOVIE_TYPE = ? WHERE CM01_MOVIE_ID=?");
			pstmt = conn.prepareStatement(sql.toString());
			pstmt.setString(1, movieName);
			pstmt.setString(2, movieDescription);
			pstmt.setInt(3, movieLenght);
			pstmt.setInt(4, movieTypeId);
			pstmt.setInt(5, movieId);
			pstmt.executeUpdate();
		}
		catch (Exception e) {
			status = false;
			e.printStackTrace();
		}
		finally {
			DBConnManager.getInstance().close(pstmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		return status;
	}
	
	
	
	public boolean deleteMovie() {
		boolean status = true;
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			conn = DBConnManager.getInstance().getConnection();
			StringBuilder sql = new StringBuilder();
			sql.append(" DELETE FROM TM01_MOVIES");
			sql.append(" WHERE CM01_MOVIE_ID=?");
			pstmt = conn.prepareStatement(sql.toString());
			pstmt.setInt(1, movieId);
			pstmt.executeUpdate();
		}
		catch (Exception e) {
			status = false;
			e.printStackTrace();
		}
		finally {
			DBConnManager.getInstance().close(pstmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		return status;
	}
	
	
	
	public static int getLastId() {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		int lastId = 0;
		try {
			conn = DBConnManager.getInstance().getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery("select max(CM01_MOVIE_ID) from TUDB.TM01_MOVIES");
			rs.next();
			lastId = rs.getInt(1);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			DBConnManager.getInstance().close(rs);
			DBConnManager.getInstance().close(stmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		return lastId;
	}
	
	
	
	public String printGenres() {
		String genres = "";
		if (this.genres != null && this.genres.size() > 0) {
			genres = this.genres.get(0).getGenreName();
			for (int i = 1; i < this.genres.size(); i++)
				genres += ", " + this.genres.get(i).getGenreName();
		}
		return genres;
	}
	
	
	
	public int getMovieId() {
		return movieId;
	}
	
	
	
	public void setMovieId(int movieId) {
		this.movieId = movieId;
	}
	
	
	
	public String getActiveFromDate() {
		return activeFromDate;
	}
	
	
	
	public void setActiveFromDate(String activeFromDate) {
		this.activeFromDate = activeFromDate;
	}
	
	
	
	public String getActiveToDate() {
		return activeToDate;
	}
	
	
	
	public void setActiveToDate(String activeToDate) {
		this.activeToDate = activeToDate;
	}
	
	
	
	public String getMovieName() {
		return movieName;
	}
	
	
	
	public void setMovieName(String movieName) {
		this.movieName = movieName;
	}
	
	
	
	public Vector<Genre> getGenres() {
		return genres;
	}
	
	
	
	public void setGenres(Vector<Genre> genres) {
		this.genres = genres;
	}
	
	
	
	public String getMovieDescription() {
		return movieDescription;
	}
	
	
	
	public void setMovieDescription(String movieDescription) {
		this.movieDescription = movieDescription;
	}
	
	
	
	public int getMovieLenght() {
		return movieLenght;
	}
	
	
	
	public void setMovieLenght(int movieLenght) {
		this.movieLenght = movieLenght;
	}
	
	
	
	public int getMovieTypeId() {
		return movieTypeId;
	}
	
	
	
	public void setMovieTypeId(int movieTypeId) {
		this.movieTypeId = movieTypeId;
	}
	
	
	
	public String getMovieType() {
		return movieType;
	}
	
	
	
	public void setMovieType(String movieType) {
		this.movieType = movieType;
	}
	
	
	
	public Vector<MovieImage> getMovieImages() {
		return movieImages;
	}
	
	
	
	public void setMovieImages(Vector<MovieImage> movieImages) {
		this.movieImages = movieImages;
	}
	
}
